package com.bigflexlib.ds.op
{
	import com.bigflexlib.ds.container.IRContainer;

	public class AbstractContainerToElementOp extends AbstractContainerOp
	{
		

		protected var iValueHandlerFunction:Function;
		
		
		protected var currentElement:*;
		
		
		protected var emptyValue:*;
		
		
		
		/**
		 * 
		 * We need this flag to cover the case where the container is empty on the first run
		 * 
		*/				
		private var firstRun:Boolean = true;		
		
		
		
		
		
		
		
		public function AbstractContainerToElementOp(  iContainer:IRContainer, iValueHandlerFunction:Function, emptyValue:* = undefined  )
		{
			
			super( iContainer );
		
			this.iValueHandlerFunction = iValueHandlerFunction;
			
			this.emptyValue = emptyValue;
		
			
			batch( );
			
			firstRun = false;

		}
		
		
		
		
		protected function batch( ):void
		{
		
		
		}
		


		
		protected function updateElement( e:* ):void
		{
		
			if ( ! firstRun ) // we make sure that the first run always calls the value handler ( to cover the case where the container is initially empty )
			{
				if ( currentElement === e )
					return;
			}

				
			currentElement = e;
			
			iValueHandlerFunction.call( null, e );
			
		}		
		
		
		

		
		override public function dispose( ):void
		{
		
			super.dispose( );
			
			iValueHandlerFunction = null;
			
			currentElement = undefined;
			
			emptyValue = undefined;
		
		}		
		
		
		
		
	}
}